﻿using System;
using System.Data;
using System.Collections.Generic;
using System.IO;
using System.Text;
using libsharp;
using libexcel;

namespace libhtml
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "导出.txt";
            StreamReader reader = null;
            if (!File.Exists(str) || (reader = new StreamReader(str, Encoding.Default)) == null)
            {
                Console.WriteLine(" 读取 {0} 失败", str);
                ConsoleSharp.continueByAnykey();
                ConsoleSharp.exitByAnykey();
                return;
            }
            Info info = new Info();
            info.read(reader);
            DataTable table = new DataTable();
            table.Columns.Add("小区名称");
            table.Columns.Add("楼宇名称");
            table.Columns.Add("单元名称");
            table.Columns.Add("房间编号");
            table.Columns.Add("地址编码");

            DataRow row = table.NewRow();
            row[0] = info.addr;
            row[1] = info.tag;
            row[4] = info.code;
            table.Rows.Add(row);
            for (int k = 0, size = info.buildings.Length; k < size; ++k)
            {
                Building building = info.buildings[k];
                row = table.NewRow();
                row[0] = info.addr;
                row[1] = building.tag2;
                row[2] = building.tag;
                row[4] = building.code;
                table.Rows.Add(row);
                for (int i = 0, size2 = building.units.Length; i < size2; ++i)
                {
                    Unit unit = building.units[i];
                    row = table.NewRow();
                    row[0] = info.addr;
                    row[1] = building.tag2;
                    row[2] = building.tag;
                    row[3] = unit.tag;
                    row[4] = unit.code;
                    table.Rows.Add(row);
                }
            }
            str = str.Split('.')[0] + ".xls";
            ExcelHelper.writeExcel(table, str);
            Console.WriteLine(" 成功{0}", str);
            ConsoleSharp.continueByAnykey();
        }
    }
}
